from typing import Generic, Optional, TypeVar

from pydantic import BaseModel

T = TypeVar("T")


class CommonResp(BaseModel, Generic[T]):
    """Common response model."""
    code: int = 0
    msg: str = "success"
    data: Optional[T] = None

    @classmethod
    def ok(cls,
           data: Optional[T] = None,
           msg: str = "success") -> "CommonResp[T]":
        """Create a success response."""
        return cls(code=0, msg=msg, data=data)

    @classmethod
    def fail(cls, msg: str = "failed", code: int = 1) -> "CommonResp[T]":
        """Create a failure response."""
        return cls(code=code, msg=msg)
